//
//  CellForClickYouJiCell2.m
//  SeeTheWorld
//
//  Created by dllo on 16/3/4.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CellForClickYouJiCell2.h"
#import "PCH.pch"
#import <UIImageView+WebCache.h>


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface CellForClickYouJiCell2 ()

@property (nonatomic, retain) UILabel *labelForContents;
// imageView.
@property (nonatomic, retain) UIImageView *imageViewForPhoto;
// imageView 宽.
@property (nonatomic, assign) NSInteger width;
// imageView 高.
@property (nonatomic, assign) NSInteger height;

@end

@implementation CellForClickYouJiCell2

- (void)dealloc {
    [_labelForContents release];
    [_imageViewForPhoto release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self createSubViews];
        
    }
    
    return self;
}

#pragma mark ** 创建子视图
- (void)createSubViews {
    
    self.imageViewForPhoto = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageViewForPhoto];
    [_imageViewForPhoto release];
    
    self.labelForContents = [[UILabel alloc] init];
    self.labelForContents.numberOfLines = 0;
    self.labelForContents.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.labelForContents];
    [_labelForContents release];
    self.labelForContents.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);

}

- (void)voluationToCellSubViewsWithString:(NSString *)string photo:(NSString *)photo width:(CGFloat)width height:(CGFloat)height {
    
    self.labelForContents.text = string;
    
    [self.imageViewForPhoto sd_setImageWithURL:[NSURL URLWithString:photo] placeholderImage:[UIImage imageNamed:@"1f178a82b9014a90e9109296ad773912b31beeeb"]];
    
    self.width = width;
    
    self.height = height;
    
}

#pragma mark ** 自适应高度
- (CGFloat)heightForImageWithWidth:(CGFloat)width height:(CGFloat)height {
    
    return height * (WIDTH - 20) / width;
}

- (CGFloat)heightForLabelWithDescription:(NSString *)description {
    
    CGRect rect = [description boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
}

- (CGFloat)heightForCellWithDescription:(NSString *)description {
    
    CGFloat height = [self heightForLabelWithDescription:description];
    
    return 5 + height + 5;
}

- (CGFloat)heightForCellWithDescription:(NSString *)description width:(CGFloat)width height:(CGFloat)height {
    
    if (description) {
        
        CGFloat labelForHeight = [self heightForLabelWithDescription:description];
        
        return 5 + labelForHeight + 5 + (height * (WIDTH - 20) / width) + 5;
    } else {
        
        
        return 5 + (height * (WIDTH - 20) / width) + 5;
    }
    
}

#pragma mark ** 布局子视图
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    if (![self.labelForContents.text isEqualToString:@"(null)"]) {
        
        if (self.imageViewForPhoto.image.size.height) {
            
            self.imageViewForPhoto.frame = CGRectMake(10, 5, WIDTH - 20, [self heightForImageWithWidth:self.width height:self.height]);
            
        } else {
            
            self.imageViewForPhoto.frame = CGRectMake(0, 0, 0, 0);
            
        }
        
        self.labelForContents.frame = CGRectMake(10, self.imageViewForPhoto.frame.origin.y + self.imageViewForPhoto.frame.size.height + 5, WIDTH - 20, [self heightForLabelWithDescription:self.labelForContents.text]);
        
    } else {
        
        if (self.imageViewForPhoto.image.size.height) {
            
            self.imageViewForPhoto.frame = CGRectMake(10, 5, WIDTH - 20, [self heightForImageWithWidth:self.width height:self.height]);
            
        } else {
            
            self.imageViewForPhoto.frame = CGRectMake(0, 0, 0, 0);
            
        }
        
        self.labelForContents.frame = CGRectMake(0, 0, 0, 0);
        
    }
    
}




@end
